package com.ruoyi.project.wx.controller;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bootdo.PageUtils;
import com.ruoyi.common.utils.bootdo.Query;
import com.ruoyi.common.utils.bootdo.R;
import com.ruoyi.project.wx.domain.ChinaAreaDO;
import com.ruoyi.project.wx.service.ChinaAreaService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 城市区域表
 * 
 * @author long
 * @email 499626561@qq.com
 * @date 2021-01-08 15:27:03
 */
 
@Controller
@RequestMapping("/wx/chinaArea")
public class ChinaAreaController {
	@Autowired
	private ChinaAreaService chinaAreaService;
	
	@GetMapping()
	@RequiresPermissions("wx:chinaArea:chinaArea")
	String ChinaArea(){
	    return "wx/chinaArea/chinaArea";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("wx:chinaArea:chinaArea")
	public PageUtils list(@RequestParam Map<String, Object> params){
	    params.put("delFlag","0");
	    params.put("sort","ca_at");
	    params.put("order","desc");
        //查询列表数据
        Query query = new Query(params);
	    if(params.containsKey("name")&& StringUtils.isNotBlank(params.get("name").toString())){
            List<ChinaAreaDO> chinaAreaList = chinaAreaService.likeList(query);
            int total = chinaAreaService.likeListCount(query);
            PageUtils pageUtils = new PageUtils(chinaAreaList, total);
            return pageUtils;
		}

		List<ChinaAreaDO> chinaAreaList = chinaAreaService.list(query);
		int total = chinaAreaService.count(query);
		PageUtils pageUtils = new PageUtils(chinaAreaList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("wx:chinaArea:add")
	String add(){
	    return "wx/chinaArea/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("wx:chinaArea:edit")
	String edit(@PathVariable("id") Long id,Model model){
		ChinaAreaDO chinaArea = chinaAreaService.get(id);
		model.addAttribute("chinaArea", chinaArea);
	    return "wx/chinaArea/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("wx:chinaArea:add")
	public R save(ChinaAreaDO chinaArea){
	    /*chinaArea.setId(UuidUtil.get32UUID());
	    chinaArea.setDelFlag(0);
	    chinaArea.setCaAt(new Date());*/
		if(chinaAreaService.save(chinaArea)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("wx:chinaArea:edit")
	public R update( ChinaAreaDO chinaArea){
		/*chinaArea.setUpAt(new Date());*/
		chinaAreaService.update(chinaArea);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("wx:chinaArea:remove")
	public R remove( Long id){
		ChinaAreaDO chinaArea = chinaAreaService.get(id);
		chinaArea.setDelFlag(1);

		if(chinaAreaService.update(chinaArea)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("wx:chinaArea:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
	   if(ids.length>0){
			for (Long id : ids) {
				ChinaAreaDO chinaArea = chinaAreaService.get(id);
				chinaArea.setDelFlag(1);

				chinaAreaService.update(chinaArea);
			}
		}
		return R.ok();
	}
	
}
